package com.fkcai.hansoul.controller;

import com.fkcai.hansoul.common.util.ResultCode;
import com.fkcai.hansoul.model.dto.UserFollowDto;
import com.fkcai.hansoul.model.vo.follow.UserListVo;
import com.fkcai.hansoul.service.UserFollowService;
import com.fkcai.hansoul.common.util.Result;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 用户关注控制器类
 */
@RestController
@RequestMapping("/api/user")
public class UserFollowController {
	@Autowired
	private UserFollowService userFollowService;
	
	
	/**
	 * 用户关注接口
	 *
	 * @param userId      用户 id
	 * @param requestBody
	 * @return 用户关注信息
	 */
	@PostMapping("/follow/{userId}")
	public Result<Void> followUser (@PathVariable Integer userId,
	                                @RequestBody @Valid UserFollowDto requestBody) {
		userFollowService.followUser(userId, requestBody.getFollowedUserId());
		
		return Result.success();
	}
	
	
	/**
	 * 用户取消关注接口
	 *
	 * @param userId      用户 id
	 * @param requestBody
	 * @return 取消关注信息
	 */
	@PostMapping("/unfollow/{userId}")
	public Result<Void> unfollowUser (@PathVariable Integer userId,
	                                  @RequestBody @Valid UserFollowDto requestBody) {
		
		userFollowService.unfollowUser(userId, requestBody.getFollowedUserId());
		
		return Result.success();
	}
	
	
	/**
	 * 获取用户关注的人数,粉丝人数和互关人数
	 *
	 * @param userId 用户 id
	 * @return 人数
	 */
	@GetMapping("/stats/{userId}")
	public Result<Map<String, Integer>> getUserStats (@PathVariable Integer userId) {
		
		try {
			Integer followingCount = userFollowService.getFollowingCount(userId);
			Integer fansCount = userFollowService.getFansCount(userId);
			Integer mutualCount = userFollowService.getMutualFollowingCount(userId);
			
			Map<String, Integer> stats = new ConcurrentHashMap<>();
			stats.put("followingCount", followingCount);
			stats.put("fansCount", fansCount);
			stats.put("mutualCount", mutualCount);
			
			return Result.success(stats);
		} catch (Exception e) {
			
			return Result.fail(ResultCode.USER_STATS_ERROR);
		}
	}
	
	
	/**
	 * 获取用户互关列表接口
	 *
	 * @param userId 用户 id
	 * @return 返回用户互关接口
	 */
	@GetMapping("/mutualList/{userId}")
	public Result<UserListVo> getMutualFollowingUsersList (@PathVariable Integer userId) {
		UserListVo data = userFollowService.getMutualFollowingUsersList(userId);
		
		return Result.success(data);
	}
	
	
	/**
	 * 获取用户粉丝列表接口
	 *
	 * @param userId 用户 id
	 * @return 粉丝列表
	 */
	@GetMapping("/fansList/{userId}")
	public Result<UserListVo> getFansUsersList (@PathVariable Integer userId) {
		UserListVo data = userFollowService.getFansUsersList(userId);
		
		return Result.success(data);
	}
	
	
	/**
	 * 获取用户关注列表接口
	 *
	 * @param userId 用户 id
	 * @return 关注列表
	 */
	@GetMapping("/followingList/{userId}")
	public Result<UserListVo> getFollowingUsersList (@PathVariable Integer userId) {
		
		UserListVo data = userFollowService.getFollowingUsersList(userId);
		
		return Result.success(data);
	}
	
}
